from django.db import models
from django.utils import timezone

# 课程模型
class Course(models.Model):
    name = models.CharField(max_length=200)

    def __str__(self):
        return self.name

    class Meta:
        verbose_name = '培训课程'
        verbose_name_plural = '培训课程'

# 培训项目模型
class Project(models.Model):
    DEFAULTDAYS = 30

    name = models.CharField(max_length=200, verbose_name='项目名称')
    begin_time = models.DateTimeField("起始时间", default=timezone.now())
    end_time = models.DateTimeField("截至时间", default=timezone.now() + timezone.timedelta(days=DEFAULTDAYS))
    courses = models.ManyToManyField(Course, verbose_name='课程')

    def courses_names(self):
        return ', '.join([course.name for course in self.courses.all()])
    courses_names.short_description = "课程名称"

    #是否在有效期内
    def is_not_end(self):
        return self.end_time > timezone.now()
    is_not_end.boolean = True
    is_not_end.admin_order_field = 'end_time'
    is_not_end.short_description = '是否在有效期内?'

    def __str__(self):
        return self.name

    class Meta:
        verbose_name = '培训项目'
        verbose_name_plural = '培训项目'

# 学员模型
class Student(models.Model):
    name = models.CharField(max_length=200)

    def __str__(self):
        return self.name

    class Meta:
        verbose_name = '学员'
        verbose_name_plural = '学员'

# 教师模型
class Teacher(models.Model):
    name = models.CharField(max_length=200)

    def __str__(self):
        return self.name

    class Meta:
        verbose_name = '导师'
        verbose_name_plural = '导师'

# 培训班级模型
class TrainingClass(models.Model):
    name = models.CharField(max_length=200)
    project = models.ForeignKey(Project)
    head_teacher = models.ForeignKey(Teacher)
    students = models.ManyToManyField(Student)

    def __str__(self):
        return self.name

    class Meta:
        verbose_name = '培训班'
        verbose_name_plural = '培训班'
